package com.hk.service.order.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.LongId;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.product.PaymentFromYm;
import com.hk.commons.entity.sys.SysUser;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.order.IPaymentFromYm;
import com.hk.service.sys.ISysUser;

@Service
public class PaymentFromYmImpl extends BaseServiceImpl<PaymentFromYm> implements
		IPaymentFromYm {
	@Autowired
	private ISysUser userService;

	@Override
	public PaymentFromYm findById(long id) {
		return getById(id);
	}

	@Override
	public Long add(PaymentFromYm model) {
		Long id = null;
		if (model != null && model.getPaymentNo() != null) {
			id = LongId.get();
			model.setId(id);
			model.setCts(System.currentTimeMillis());
			if (StringHelper.isNotEmpty(model.getAccountName())) {
				model.setAccountName(model.getAccountName().trim());
			} else if (model.getUserId() != null) {
				SysUser user = userService.findById(model.getUserId());
				if (user != null && StringHelper.isNotEmpty(user.getRealName())) {
					model.setAccountName(user.getRealName());
				}
			}
			model.setPaymentNo(model.getPaymentNo().trim());
			super.save(model);
		}

		return id;
	}

}
